package com.xb.controller;

import com.xb.entity.Menu;
import com.xb.service.IMenuService;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Controller
 * /api/menu
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    /**
     * 根据邮箱获取菜单
     */
    @RequestMapping("/getMenuByUser")
    public Result getMenuByUser(HttpSession session) {
        Result result = new Result();
        String email = (String) session.getAttribute("email");
        List<Menu> menus = menuService.selectMenuByEmail(email);
        if (menus != null) {
            result.setData(menus);
            result.setStatus(Result.RESPONSE_SUCCESS);
        } else {
            result.setStatus(Result.RESPONSE_FAIL);
            result.setMessage("未找到当前用户对应菜单");
        }
        return result;

    }

    @GetMapping("/getMenu")
    public Result getMenuTree(){
        return menuService.getMenuTree();
    }

    @GetMapping
    public Result getMenu(Integer parentId){
        return menuService.getMenu(parentId);
    }

    @RequestMapping("/page")
    @RequiresPermissions("system:menu:page")
    public Object findPage(Menu obj,@RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "limit",defaultValue = "10")Integer limit) {
        return menuService.selectMenuList(obj, page, limit);
    }

    /**
     * 添加
     */
    @PostMapping
    @RequiresPermissions("system:menu:add")
    public Object add(@RequestBody Menu obj,HttpSession session) {
        return menuService.add(obj,session);
    }

    /**
     * 修改
     */
    @PutMapping
    @RequiresPermissions("system:menu:update")
    public Object update(@RequestBody Menu obj) {
        return menuService.update(obj);
    }
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("system:menu:del")
    public Object del(@PathVariable Integer id) {
        return menuService.deleteMenuById(id);
    }
}
